import * as React from "react";
import { useHistory } from "react-router-dom";

import style from "./style.module.css";
import Toast from "../../components/Toast";
import * as api from "../../services/user.services";
import IconFont from "../../assets/icon";
import { Input } from "antd";

export default function Login() {
  const history = useHistory();
  const [username, setUsername] = React.useState<string>();
  const [password, setPassword] = React.useState<string>();

  const login = async () => {
    try {
      let result = await api.userlogin(username, password);
      if (result.stat === "ERR_EXISTS") {
        history.push("/mine");
      } else if (result.stat === "ok") {
        Toast("当前所用用户名没有注册，是否要注册");
      } else {
        Toast(result.message);
      }
    } catch {}
  };
  return (
    <div className={style.wrap}>
      <h3 className={style.title}>BOSS直聘</h3>
      <Input
        prefix={<IconFont className={style.icon} type="icon-shouji" />}
        placeholder="账号"
        className={style.inputText}
        onChange={(event) => setUsername(event.target.value)}
      ></Input>
      <Input
        type="password"
        prefix={<IconFont className={style.icon} type="icon-xinfengyoujian" />}
        placeholder="密码"
        className={style.inputText}
        onChange={(event) => setPassword(event.target.value)}
      ></Input>
      <button className={style.button} onClick={login}>
        注 册/登 录
      </button>
    </div>
  );
}
